//index.js
//获取应用实例
const app = getApp()
const homeService = require('../../service/home.js');
const contentService = require('../../service/content.js');

Page({
  data: {
    title:"推荐",
    queryParams:{
      page:1,
      num:10,
      site:"",
      labelId: "",
      type: 1,
    },
    dynamicList:[],
    isLast:false,
    loading:false,
    isShowBackTop:false,
    labellife:"label-detail-btn",
    labelfun:"label-detail-btn",
    labelfoot:"label-detail-btn",
    labeltravel:"label-detail-btn"
  },
  onLoad: function () {
    this.getDynamicList();
  },
  getDynamicList(){
    let d = {
      ...this.data.queryParams,
    }
    this.setData({
      loading:true
    })
    contentService.getContentList(d).then(res => {
      if (res.resCode == 0){
        if (!res.object.length){
          this.setData({
            isLast:true
          })
          return false;
        }
        let d = this.data.dynamicList.concat(res.object);
        this.setData({
          dynamicList:d,
        })
        this.waterfallComponent = this.selectComponent("#waterfall");
        if (!this.data.dynamicList.length){
          this.waterfallComponent.start(res.object).then(res => {
          })
        }else{
          this.waterfallComponent.append(res.object).then(res => {})
        }
      }
    }).finally(()=>{
      this.setData({
        loading: false
      })
    })
  },
  onReachBottom(){
    if(this.data.isLast) return false;
    this.setData({
      "queryParams.page": ++this.data.queryParams.page
    })
    this.getDynamicList();
  },
  getContentBySite(e){
    let detailValue = e.detail;
    //去重操作
    if (this.data.queryParams.site == detailValue.site){
      return false;
    }
    this.waterfallComponent.delete(0,this.data.dynamicList.length);
    //reset 
    this.setData({
      dynamicList:[],
      "queryParams.page":1,
      isLast:false,
      "queryParams.site": detailValue.site || ""
    });
    this.getDynamicList();
  },
  onPageScroll:function(e){
    let deviceInfo = app.globalData.deviceInfo;
    if (e.scrollTop > deviceInfo.screenHeight +50){
      this.setData({
        isShowBackTop:true
      })
    }else{
      this.setData({
        isShowBackTop:false
      })
    }
  },
  labelSelect: function (event) {
    var gid = event.currentTarget.dataset.gId;
    this.data.queryParams.labelId = gid;
    console.log(gid)
    //reset 
    this.setData({
      dynamicList: [],
      "queryParams.page": 1,
      isLast: false
    });
    this.getDynamicList();

    if (gid == 0) {
      this.setData({
        labellife: "label-detail-btn-select",
        labelfun: "label-detail-btn",
        labelfoot: "label-detail-btn",
        labeltravel: "label-detail-btn",
      })
    } else if (gid == 1) {
      this.setData({
        labellife: "label-detail-btn",
        labelfun: "label-detail-btn-select",
        labelfoot: "label-detail-btn",
        labeltravel: "label-detail-btn",
      })
    } else if (gid == 2) {
      this.setData({
        labellife: "label-detail-btn",
        labelfun: "label-detail-btn",
        labelfoot: "label-detail-btn-select",
        labeltravel: "label-detail-btn",
      })
    } else if (gid == 3) {
      this.setData({
        labellife: "label-detail-btn",
        labelfun: "label-detail-btn",
        labelfoot: "label-detail-btn",
        labeltravel: "label-detail-btn-select",
      })
    }

  }


})
